import axios from 'axios'
import moment from 'moment'
import {creatSignWrap} from '@/myutl/utlenc'
import env from '@/config/env'

export const apiGetRentUser = async (custId, authToken = "authToken", needSign = true) => {
  let curTs = moment().format('YYYYMMDDHHmmss');
  let params = {
    "appId": env.appId,
    "charset": "utf-8",
    "timestamp": curTs,
    "version": "1.0",
    "appAuthToken": authToken,
    "bizContent": {
      "custId": custId,
      "msgSsn": curTs
    }
  };
  if (needSign) {
    let ret = await creatSignWrap(params);
    if (!ret.isRetSuss) {
      console.log("resolve");
      return new Promise((resolve, reject) => {
        resolve({data: {subCode: "EA0001", subMsg: "签名失败"}});
      });
    }
    params.signType = "RSA2";
    params.sign = ret.data;
  }
  console.log("apiGetRentUser:", params);
  return axios.get('/rent/userQuery', {params});
}

export const apiGetRentContList = async (custId, authToken = "authToken", needSign = true) => {
  let curTs = moment().format('YYYYMMDDHHmmss');
  let params = {
    "appId": env.appId,
    "charset": "utf-8",
    "timestamp": curTs,
    "version": "1.0",
    "appAuthToken": authToken,
    "bizContent": {
      "queryType": "0",
      "mchtCd": env.mchtCd,
      "custId": custId,
      "msgSsn": curTs
    }
  };
  if (needSign) {
    let ret = await creatSignWrap(params);
    if (!ret.isRetSuss) {
      console.log("resolve");
      return new Promise((resolve, reject) => {
        resolve({data: {subCode: "EA0001", subMsg: "签名失败"}});
      });
    }
    params.signType = "RSA2";
    params.sign = ret.data;
  }
  console.log("apiGetRentContList:", params);
  return axios.get('/rent/contractQuery', {params});
}

/**
 * [description]
 * @param  {[type]} params [description]
 *                         landlordName
 * @return {[type]}        [description]
 */
export const apiNewRentCont = async (contInf, authToken = "authToken", needSign = true) => {
  console.log("apiNewRentCont:", contInf);
  let curTs = moment().format('YYYYMMDDHHmmss');
  let params = {
    "appId": env.appId,
    "charset": "utf-8",
    "timestamp": curTs,
    "version": "1.0",
    "appAuthToken": authToken,
    "bizContent": {
      "contractTp": "0",  // 0-新合同
      "mchtCd": env.mchtCd,
      "custId": contInf.custId,
      "landlordName": contInf.landlordName,
      "landlordPhone": contInf.landlordPhone,
      "landlordCard": contInf.landlordCard,
      "houseAddr": contInf.houseAddr,
      "rentMons": contInf.rentMons,
      "rental": contInf.rental,
      "startDate": contInf.startDate,
      "endDate": contInf.endDate,
      "payDate": contInf.payDate,
      "payFreq": contInf.payFreq,
      "img": contInf.img,
      "msgSsn": curTs
    }
  };
  if (needSign) {
    let ret = await creatSignWrap(params);
    if (!ret.isRetSuss) {
      console.log("resolve");
      return new Promise((resolve, reject) => {
        resolve({data: {subCode: "EA0001", subMsg: "签名失败"}});
      });
    }
    params.signType = "RSA2";
    params.sign = ret.data;
  }
  console.log("apiNewRentCont:", params);
  return axios.get('/rent/contractCreate', {params});
}

export const apiUploadContImg = async (formdata) => {
  return axios.post('/rent/fileUpload', formdata,
                    {headers:{'Content-Type':'multipart/form-data'}});
}

/**
 * 根据请求参数的不同查询订单信息
 * @param custId 客户编号
 * @param mchtOdrId 商户订单号
 * @param odrStatus 订单状态
 * @param authToken
 * @param needSign 是否需要签名
 * @returns {Promise<*>}
 */
export const apiGetOrderList = async (custId, mchtOdrId, odrStatus, authToken = "authToken", needSign = true) => {
  let curTs = moment().format('YYYYMMDDHHmmss');
  let params = {
    "appId": env.appId,
    "charset": "utf-8",
    "timestamp": curTs,
    "version": "1.0",
    "appAuthToken": authToken,
    "bizContent": {
      "custId": custId,
      "msgSsn": curTs
    }
  };
  if (needSign) {
    let ret = await creatSignWrap(params);
    if (!ret.isRetSuss) {
      console.log("resolve");
      return new Promise((resolve, reject) => {
        resolve({data: {subCode: "EA0001", subMsg: "签名失败"}});
      });
    }
    params.signType = "RSA2";
    params.sign = ret.data;
  }
  if (mchtOdrId) {
    params.mchtOdrId = mchtOdrId;
  }
  if (odrStatus) {
    params.odrStatus = odrStatus;
  }
  console.log("apiGetOrderList:", params);
  return axios.get('/rent/orderQuery', {params});
}

/**
 * 更新订单信息
 * @param orderInf 需要更新的订单参数
 * @param authToken
 * @param needSign
 * @returns {Promise<*>}
 */
export const apiUpdateOrder = async (orderInf, authToken = "authToken", needSign = true) => {
  console.log("apiUpdateOrder:", orderInf);
  let curTs = moment().format('YYYYMMDDHHmmss');
  let params = {
    "appId": env.appId,
    "charset": "utf-8",
    "timestamp": curTs,
    "version": "1.0",
    "appAuthToken": authToken,
    "bizContent": {
      "msgSsn": curTs,
      "odrId": orderInf.odrId,
      "odrStatus": orderInf.odrStatus,
      "otherAmt": orderInf.otherAmt,
      "otherAmtDsp": orderInf.otherAmtDsp
    }
  };
  if (needSign) {
    let ret = await creatSignWrap(params);
    if (!ret.isRetSuss) {
      console.log("resolve");
      return new Promise((resolve, reject) => {
        resolve({data: {subCode: "EA0001", subMsg: "签名失败"}});
      });
    }
    params.signType = "RSA2";
    params.sign = ret.data;
  }
  console.log("apiUpdateOrder:", params);
  return axios.get('/rent/orderManage', {params});
}
